package com.example.parkingproject.exceprionHandler;

import com.example.parkingproject.code.ServiceCode;
import com.example.parkingproject.ex.ServiceException;
import com.example.parkingproject.result.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ServiceExceptionHandler {

    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e){
        String message ="服务器出错啦，稍后再试";
        e.printStackTrace();
        return JsonResult.fail(ServiceCode.ERROR_NOT_FOUND,message);
    }
    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){
        return JsonResult.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler
    public JsonResult BadCredentialsException(BadCredentialsException e){
        return JsonResult.fail(ServiceCode.error_bad_request,"用户名或密码错误");
    }
    @ExceptionHandler
    public JsonResult DisabledException(DisabledException e){
        return JsonResult.fail(ServiceCode.error_bad_request,"用户已失效");
    }
    @ExceptionHandler
    public JsonResult AccessDeniedException(AccessDeniedException e){
        return JsonResult.fail(ServiceCode.error_bad_request,"您的权限不够");
    }
    @ExceptionHandler
    public JsonResult InternalAuthenticationServiceException(InternalAuthenticationServiceException e){
        return JsonResult.fail(ServiceCode.error_bad_request,e.getMessage());
    }
}
